using System;
using System.IO;
using System.Text;
using SexpSharp;
using SexpSharp.Serialization;
using NUnit.Framework;

namespace SexpSharp.Serialization.Tests
{
	[TestFixture]
	public class Test2
	{
		[Test]
		public void a() {
			serialize(new Sample1());
		}

		[Test]
		public void b() {
			serialize(new Sample2().InitMe());
		}

		[Test]
		public void c() {
			serialize(new Sample3());
		}

		[Test]
		public void d() {
			serialize(new Fixed1().InitMe());
		}

		[Test]
		public void e() {
			serialize(new Fixed2().InitMe());
		}

		[Test]
		public void f() {
			deserialize("(serial-object #01# (SexpSharp.Serialization.Tests.Sample1 TestObjects ((b (.b True)) (s (.s2 #002a#)) (i (.s4 #fffffc16#)) (l (.s8 #ffffffffffff8300#)) (str (.s \"I am the very model of a modern major general\")) (d (.D \"3.141590e+000\")))))");
		}

		[Test]
		public void g() {
			deserialize("(serial-object #01# (SexpSharp.Serialization.Tests.Sample2 TestObjects ((b (.b True)) (s (.s2 #002a#)) (i (.s4 #fffffc16#)) (l (.s8 #ffffffffffff8300#)) (str (.s \"I am the very model of a modern major general\")) (d (.D \"3.141590e+000\")))))");
		}

		[Test]
		public void h() {
		 	deserialize("(serial-object #01# (sample-type-name TestObjects ((some-bool (.b True)) (some-short (.s2 #002a#)) (some-int (.s4 #fffffc16#)) (some-long (.s8 #ffffffffffff8300#)) (some-string (.s \"I am the very model of a modern major general\")) (some-decimal (.D \"3.141590e+000\")))))");
		}

		[Test]
		public void i() {
			deserialize("(serial-object #01# (SexpSharp.Serialization.Tests.Fixed1 TestObjects (True #002a# #fffffc16# #ffffffffffff8300# \"I am the very model of a modern major general\" \"3.141590e+000\")))");
		}

		[Test]
		public void j() {
		 	deserialize("(serial-object #01# (new-type-name TestObjects (True #002a# #fffffc16# #ffffffffffff8300# \"I am the very model of a modern major general\" \"3.141590e+000\")))");
		}

        [Test]
        public void k()
        {
            TestEnum e = TestEnum.ValueOne;
            serialize(e);
            e = TestEnum.ValueTwo;
            serialize(e);
            e = TestEnum.ValueThree;
            serialize(e);
        }

        [Test]
        public void l()
        {
            TestEnum2 e = TestEnum2.ValueOne;
            serialize(e);
            e = TestEnum2.ValueTwo;
            serialize(e);
            e = TestEnum2.ValueThree;
            serialize(e);
        }

        [Test]
        public void m()
        {
            Cycle1 c1 = new Cycle1();
            Cycle2 c2 = new Cycle2();
            c1.cycle = c2;
            c1.foo = 1234;
            c2.cycle = c1;
            c2.foo = 4321;
            serialize(c1);
        }

		static void deserialize(string expr)
        {
            MemoryStream s = new MemoryStream(new ASCIIEncoding().GetBytes(expr));
            object o = new SexpFormatter().Deserialize(s);
			Console.Error.WriteLine(o);
        }

        static void serialize(object o)
        {
            SexpFormatter f = new SexpFormatter();
            f.SerializeFormat = Format.Advanced;
            MemoryStream s = new MemoryStream();
            f.Serialize(s, o);
            s.Position = 0;
            object o2 = f.Deserialize(s);
            Assert.AreEqual(o, o2);
        }
	}
}